#Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
cmake_minimum_required(VERSION 3.5.1)
message(STATUS "CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}")
message(STATUS "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}")
project(op_tiling)

if("x${CMAKE_CXX_COMPILER}" STREQUAL "x")
    set(CMAKE_CXX_COMPILER g++)
endif()
if("x${CMAKE_C_COMPILER}" STREQUAL "x")
    set(CMAKE_C_COMPILER gcc)
endif()

execute_process(
    COMMAND mkdir -p ../include/external/ge
    COMMAND cp $ENV{ASCEND_HOME_PATH}/compiler/include/ge/ge_api_error_codes.h ../include/external/ge
)

set(ASCEND_INCLUDE_DIR $ENV{ASCEND_HOME_PATH}/tools/msopgen/template/custom_operator_sample/TIK/PyTorch)
include_directories(
    ../include
    $ENV{ASCEND_HOME_PATH}/compiler/include
    $ENV{CANNDEV_DIR}/third_party/fwkacllib/inc
    ${ASCEND_INCLUDE_DIR}
    ${ASCEND_INCLUDE_DIR}/metadef/inc
    ${ASCEND_INCLUDE_DIR}/metadef/inc/external
)

set(OP_TILING_TARGET "optiling")
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS_RELEASE "-fPIC -O2 -Wall -s")
set(CMAKE_CXX_FLAGS_DEBUG "-fPIC -O0 -g -Wall")
set(CMAKE_SKIP_RPATH TRUE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro,-z,now,-z,noexecstack -pie")
add_compile_options(
    -fstack-protector-all
    -D_FORTIFY_SOURCE=2 -O2
    )
#add source files
aux_source_directory(. SRCS)

if("x${SRCS}" STREQUAL "x")
    add_custom_target(${OP_TILING_TARGET}
            COMMAND mkdir -p ${OP_TILING_TARGET_OUT_DIR}
            COMMAND echo "no source to make lib${OP_TILING_TARGET}.so")
    return(0)
endif()

set(LIBRARY_OUTPUT_PATH ${OP_TILING_TARGET_OUT_DIR})

message(STATUS "OP_TILING_TARGET=${OP_TILING_TARGET}")
add_library(${OP_TILING_TARGET} SHARED ${SRCS})

target_link_libraries(${OP_TILING_TARGET} $ENV{ASCEND_TENSOR_COMPILER_INCLUDE}/../lib64/libgraph.so)
